
package com.huaweicloud.saaslanding.install.properties;

import com.huaweicloud.saaslanding.basic.enumeration.IdaasEnum;
import com.huaweicloud.saaslanding.basic.utils.CheckNullUtil;
import com.huaweicloud.saaslanding.install.constant.ModelConstant;

import lombok.Data;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * VPC信息
 *
 * @since 2022-03-24
 */
@Data
@Component
@ConfigurationProperties(prefix = ModelConstant.VPC_PREFIX)
public class Vpc {
    /**
     * 是否启用SSL
     */
    @Value("${saaslanding-install.ssl.enabled:false}")
    private Boolean sslEnabled;

    /**
     * idaas 类型
     */
    @Value("${saaslanding-install.idaas.type:oneaccess}")
    private String idaasType;

    /**
     * 是否启用单体应用 Https（CloudLink）
     */
    @Value("${saaslanding-install.idaas.application-https-mono-cloud-link.enabled:false}")
    private Boolean httpsCloudLinkEnabled;

    /**
     * 是否启用单体应用 Https（license）
     */
    @Value("${saaslanding-install.idaas.application-https-mono-license.enabled:false}")
    private Boolean httpsLicenseEnabled;

    private ServerVpc serverVpc = new ServerVpc();

    private ClientVpc clientVpc = new ClientVpc();

    @Data
    public static class ServerVpc {
        /**
         * 服务器端VPC UUID
         */
        private String id;

        /**
         * 服务器端VPC名称
         */

        private String name;

        /**
         * 服务器端VPC 主网段
         */
        private String cidr;

        /**
         * 服务器端VPC 子网网段位置
         */
        private String subnetIndex;

        /**
         * 模块License服务器 子网 UUID
         */
        private String licenseSubnetId;

        /**
         * 模块License服务器 子网名称
         */
        private String licenseSubnetName;
    }

    @Data
    private static class ClientVpc {

        /**
         * 客户端VPC UUID
         */
        private String id;

        /**
         * 客户端VPC名称
         */
        private String name;

        /**
         * 客户端VPC 主网段
         */
        private String cidr;

        /**
         * 客户端VPC 子网网段位置
         */
        private String subnetIndex;

        /**
         * 客户端服务器 安全组UUID
         */
        private String clientSecurityGroupId;

        /**
         * 客户端服务器 安全组名称
         */
        private String clientSecurityGroupName;

        /**
         * 文件系统 安全组UUID
         */
        private String sfsSecurityGroupId;

        /**
         * 文件系统 安全组名称
         */
        private String sfsSecurityGroupName;
    }

    public String getServerVpcId() {
        if ((isSslOneaccessLicense() || isNoSslOneaccessLicense() || isSslKeycloakLicense())
            && CheckNullUtil.isNotNull(serverVpc.id)) {
            return serverVpc.id;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）部署或未配置服务器端VPC ID(serverVpcId)");
        }
    }

    public String getServerVpcName() {
        if ((isSslOneaccessLicense() || isNoSslOneaccessLicense() || isSslKeycloakLicense())
            && CheckNullUtil.isNotNull(serverVpc.name)) {
            return serverVpc.name;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）部署或未配置服务器端VPC名称(serverVpcName)");
        }
    }

    public String getServerVpcDestination() {
        if ((isSslOneaccessLicense() || isNoSslOneaccessLicense() || isSslKeycloakLicense())
            && CheckNullUtil.isNotNull(serverVpc.cidr)) {
            return serverVpc.cidr;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）部署或未配置服务器端VPC主网段(serverVpcDestination)");
        }
    }

    public String getServerVpcSubnetIndex() {
        if ((isSslOneaccessLicense() || isNoSslOneaccessLicense() || isSslKeycloakLicense())
            && CheckNullUtil.isNotNull(serverVpc.subnetIndex)) {
            return serverVpc.subnetIndex;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）部署或未配置服务器端VPC子网网段位置(serverVpcSubnetIndex)");
        }
    }

    public String getLicenseSubnetId() {
        if ((isSslOneaccessLicense() || isNoSslOneaccessLicense() || isSslKeycloakLicense())
            && CheckNullUtil.isNotNull(serverVpc.licenseSubnetId)) {
            return serverVpc.licenseSubnetId;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）部署或未配置模块License服务器子网UUID(licenseSubnetId)");
        }
    }

    public String getLicenseSubnetName() {
        if ((isSslOneaccessLicense() || isNoSslOneaccessLicense() || isSslKeycloakLicense())
            && CheckNullUtil.isNotNull(serverVpc.licenseSubnetName)) {
            return serverVpc.licenseSubnetName;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）部署或未配置模块License服务器子网名称(licenseSubnetName)");
        }
    }

    public String getClientVpcId() {
        if ((isSslMono() || isNoSslMono()) && CheckNullUtil.isNotNull(clientVpc.id)) {
            return clientVpc.id;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）和单体应用（云联）部署或未配置客户端VPC UUID(clientVpcId)");
        }
    }

    public String getClientVpcName() {
        if ((isSslMono() || isNoSslMono()) && CheckNullUtil.isNotNull(clientVpc.name)) {
            return clientVpc.name;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）和单体应用（云联）部署或未配置客户端VPC名称(clientVpcName)");
        }
    }

    public String getClientVpcDestination() {
        if ((isSslMono() || isNoSslMono()) && CheckNullUtil.isNotNull(clientVpc.cidr)) {
            return clientVpc.cidr;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）和单体应用（云联）部署或未配置客户端VPC主网段(clientVpcDestination)");
        }
    }

    public String getClientVpcSubnetIndex() {
        if ((isSslMono() || isNoSslMono()) && CheckNullUtil.isNotNull(clientVpc.subnetIndex)) {
            return clientVpc.subnetIndex;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）和单体应用（云联）部署或未配置客户端VPC子网网段位置(clientVpcSubnetIndex)");
        }
    }

    public String getClientSecurityGroupId() {
        if ((isSslMono() || isNoSslMono()) && CheckNullUtil.isNotNull(clientVpc.clientSecurityGroupId)) {
            return clientVpc.clientSecurityGroupId;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）和单体应用（云联）部署或未配置客户端服务器安全组UUID(clientSecurityGroupId)");
        }
    }

    public String getClientSecurityGroupName() {
        if ((isSslMono() || isNoSslMono()) && CheckNullUtil.isNotNull(clientVpc.clientSecurityGroupName)) {
            return clientVpc.clientSecurityGroupName;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）和单体应用（云联）部署或未配置客户端服务器安全组名称(clientSecurityGroupName)");
        }
    }

    public String getSfsSecurityGroupId() {
        if ((isSslMono() || isNoSslMono()) && CheckNullUtil.isNotNull(clientVpc.sfsSecurityGroupId)) {
            return clientVpc.sfsSecurityGroupId;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）和单体应用（云联）部署或未配置文件系统安全组UUID(sfsSecurityGroupId)");
        }
    }

    public String getSfsSecurityGroupName() {
        if ((isSslMono() || isNoSslMono()) && CheckNullUtil.isNotNull(clientVpc.sfsSecurityGroupName)) {
            return clientVpc.sfsSecurityGroupName;
        } else {
            throw new IllegalArgumentException("未启用单体应用（License）和单体应用（云联）部署或未配置文件系统安全组名称(sfsSecurityGroupName)");
        }
    }

    private boolean isNoSslOneaccessLicense() {
        return !sslEnabled && IdaasEnum.IdaasTypeEnum.ONE_ACCESS.getName().equals(idaasType);
    }

    private boolean isSslKeycloakLicense() {
        return sslEnabled && IdaasEnum.IdaasTypeEnum.KEY_CLOAK.getName().equals(idaasType) || isKeyCloakEnabled();
    }

    private boolean isSslOneaccessLicense() {
        return sslEnabled && IdaasEnum.IdaasTypeEnum.ONE_ACCESS.getName().equals(idaasType) && httpsLicenseEnabled
            && CheckNullUtil.isNull(serverVpc.id);
    }

    private boolean isOneAccessHttpsMono() {
        return IdaasEnum.IdaasTypeEnum.ONE_ACCESS.getName().equals(idaasType)
            && (httpsLicenseEnabled || httpsCloudLinkEnabled);
    }

    private boolean isKeycloakHttpsMono() {
        return IdaasEnum.IdaasTypeEnum.KEY_CLOAK.getName().equals(idaasType);
    }

    private boolean isSslOrKeycloakEnabled() {
        return sslEnabled || isKeyCloakEnabled();
    }

    private boolean isSslMono() {
        return isSslOrKeycloakEnabled() && (isOneAccessHttpsMono() || isKeycloakHttpsMono());
    }

    private boolean isNoSslMono() {
        return !sslEnabled || isKeyCloakEnabled();
    }

    private boolean isKeyCloakEnabled() {
        return IdaasEnum.IdaasTypeEnum.KEY_CLOAK.getName().equals(idaasType);
    }

}
